package com.tg.service.protoType;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration//注意：需要通过@Configuration实现装配
public class PrototypeEntity {

    public PrototypeEntity() {
        System.out.println("初始化");
    }

    @Scope(value = "singleton")
    @Primary
    @Bean(value = "PrototypeEntity-singleton")
    public PrototypeEntity build1() {
        PrototypeEntity entity = new PrototypeEntity();
        entity.setAmount(1);
        return entity;
    }

    @Scope(value = "prototype")
    @Bean(value = "PrototypeEntity-prototype")
    public PrototypeEntity build2() {
        PrototypeEntity entity = new PrototypeEntity();
        entity.setAmount(1);
        return entity;
    }

    private Integer amount;

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"amount\":").append(amount);
        sb.append('}');
        return sb.toString();
    }
}
